#!/usr/bin/env python
# coding: utf-8
'''
@deprecated: 直接使用 onshs.BaseSuite, 不推荐直接导入该模块.
'''

import spec
import logging
import os
import sys
import time

class BaseSuite (object) :
    """ 所有 Suite 初始化代码必须从该基类继承.
    """

    OPTIONS = [
    ]
    
    def __init__(self) :
        ''' 初始化函数.
        
        派生类必须使用 BaseSuite.__init__(self) 来初始化基类
        '''
        
        self.opt_parser = spec.OptionParser()
        
        for opt in self.OPTIONS :
            if opt['required'] :
                self.opt_parser.add_option(opt['opt'],dest=opt['dest'],
                                       metavar=opt['metavar'],
                                       help=opt['help'],
                                       required=opt['required']
                                      )
            else :
                self.opt_parser.add_option(opt['opt'],dest=opt['dest'],
                                       metavar=opt['metavar'],
                                       help=opt['help'],
                                       required=opt['required'],
                                       default=opt['default']
                                      )
        
        argv = []
        append_next_arg = False
        #print sys.argv
        for i in range(1, len(sys.argv)) :
            arg = sys.argv[i]
            
            #print i, arg, arg_next
            
            if arg=='--help' or arg=='-h' :
                argv.append(arg)
                break
            
            for opt in self.OPTIONS :
                if arg == opt['opt']:
                    #print arg
                    argv.append(arg)
                    try :
                        argv.append(sys.argv[i+1])
                    except :
                        pass
        
        # print argv
        options,args = self.opt_parser.parse_args(argv)
        
        self.options = options
        """ 存放命令行选项的值.
        
        self.options.dest 引用命令行选项的值, 如:
          {
            'opt' : '--ne1.ip',
            'dest': 'ne1_ip',
            'metavar' : 'IP',
            'help' : 'the IP address of net element 1',
            'required' : True,
          }
        
        self.options.ne1_ip 用于引用 --ne1.ip 传入的选项值
        """
        
        self.args = args
        
        # logger configurations
        logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s %(levelname)-8s %(name)-16s : %(message)s',
                        filemode='w')

        suite_name = self.get_suite_name()
        self.logger = logging.getLogger(suite_name) # logs will print to stdout.
    
    def get_suite_name(self) :
        ''' 获得这个 Suite 的名字 (即 Suite 目录名)
        '''
        
        return os.path.splitext(os.path.split(os.path.split(sys.argv[0])[1])[1])[0]
    
    def init(self) :
        ''' 抽象接口, Suite 初始化代码主入口.
        
        派生类需要实现该接口.
        '''
        
        abstract
    
    